package com.thinkgem.dingdan.test.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.aliyun.oss.OSSClient;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;

public class QiNiuUtil {

	private static String preffix = "http://phe7pr76y.bkt.clouddn.com/";
	
	public static void main(String[] args) {
		/*//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
		String accessKey = "CRf6xmNnauo44p65ILxKby46IUZDPxTyp1vWiF4D";
		String secretKey = "oyvp9105qJHGGwqc5SQ_SGAbOVoiGUKNCsLj2eCJ";
		String bucket = "order";
		Auth auth = Auth.create(accessKey, secretKey);
		BucketManager bucketManager = new BucketManager(auth, cfg);
		//文件名前缀
		String prefix = "";
		//每次迭代的长度限制，最大1000，推荐值 1000
		int limit = 1000;
		//指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
		String delimiter = "";
		//列举空间文件列表
		//<img alt="" src="http://phe7pr76y.bkt.clouddn.com/1542361360707.png" style="width: 40px;height: 40px;">
		BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
		while (fileListIterator.hasNext()) {
		    //处理获取的file list结果
		    FileInfo[] items = fileListIterator.next();
		    for (FileInfo item : items) {
		    	String url = preffix+item.key;
		        System.out.println("key "+url);
		        try {
					byte[] imgeBy = imageByte(url);
					if(imgeBy != null){
						updatealiyun(imgeBy, url);
					}
					
				} catch (IOException e) {
					e.printStackTrace();
				}
		        System.out.println("hash "+item.hash);
		        System.out.println("fsize "+item.fsize);
		        System.out.println("mimeType "+item.mimeType);
		        System.out.println("putTime "+item.putTime);
		        System.out.println("endUser "+item.endUser);
		    }
		}*/
		/*String url = "http://phe7pr76y.bkt.clouddn.com/1541701711777.png";
		try {
			byte[] imgeBy = imageByte(url);
			updatealiyun(imgeBy, url);
		} catch (IOException e) {
			e.printStackTrace();
		}
*/
	        String accessKey = "CRf6xmNnauo44p65ILxKby46IUZDPxTyp1vWiF4D";
	        String secretKey = "oyvp9105qJHGGwqc5SQ_SGAbOVoiGUKNCsLj2eCJ";
	      //构造一个带指定Zone对象的配置类
	        Configuration cfg = new Configuration(Zone.zone0());
	        //...其他参数参考类注释
	        String fromBucket = "order";
	        String fromKey = "1541701711777.png";
	        String toBucket = "test";
	        String toKey = "1541701711777.png";
	        Auth auth = Auth.create(accessKey, secretKey);
	        
	      //构造一个带指定Zone对象的配置类
	        //...其他参数参考类注释
	        BucketManager bucketManager = new BucketManager(auth, cfg);
	        try {
	            bucketManager.copy(fromBucket, fromKey, toBucket, toKey);
	        } catch (QiniuException ex) {
	            //如果遇到异常，说明复制失败
	            System.err.println(ex.code());
	        }

	}
	
	
	
	public static void updatealiyun(byte[] image,String url){
		// Endpoint以杭州为例，其它Region请按实际情况填写。
		String endpoint = "https://oss-cn-shenzhen.aliyuncs.com";
		// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
		String accessKeyId = "LTAInAQzg1F7CDAv";
		String accessKeySecret = "6z2ut9d1TSjWHqJqZWduMhujkfGh5g";

		// 创建OSSClient实例。
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);

		// 上传Byte数组。
		String fileName = url.substring(url.lastIndexOf(".com")+5);
		
		byte[] content = image;
		ossClient.putObject("order9527", fileName, new ByteArrayInputStream(content));

		// 关闭OSSClient。
		ossClient.shutdown();
	}
	
	public static byte[] imageByte(String url)throws IOException{
		byte[] data = null;
		//1,导包
		//2,得到HttpClient对象
			HttpClient client = new DefaultHttpClient();
			
		//3,设置请求方式
			HttpGet get = new HttpGet(url);
		
		//4,执行请求, 获取响应信息
			HttpResponse response = client.execute(get);
			
			if(response.getStatusLine().getStatusCode() == 200)
			{
				//得到实体
				HttpEntity entity = response.getEntity();
				data = EntityUtils.toByteArray(entity);
			}
			return data;
		}
	
}
